package com.nick.sscplan.provider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SscDataBase {

    public static final String DATABASE_NAME = "ssc.db";
    public static final String TABLE_NAME = "ssc_table";
    private static final int VERSION = 1;
    private static final String CREATE_SQL = "create table " + TABLE_NAME + "("
            + "_id integer primary key autoincrement,"
            + "issue integer unique not null," + "data1 integer not null,"
            + "data2 integer not null," + "data3 integer not null,"
            + "data4 integer not null," + "data5 integer not null,"
            + "opentime text,"
            + "remark text)";

    public SQLiteDatabase mDatabase = null;

    public SscDataBase(Context context) {
        SscDataBaseHelper sdb = new SscDataBaseHelper(context, DATABASE_NAME,
                null, VERSION);
        try {
            mDatabase = sdb.getWritableDatabase();
        } catch (Exception e) {
            mDatabase = sdb.getReadableDatabase();
        }
    }

    class SscDataBaseHelper extends SQLiteOpenHelper {

        public SscDataBaseHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_SQL);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop if exists " + TABLE_NAME);
            onCreate(db);
        }
    }

    public void close() {
        mDatabase.close();
    }

}
